# Copyright (c) 2011 anatanokeitai.com(sakurai_youhei)
# 
# Permission is hereby granted, free of charge, to any person obtaining a
# copy of this software and associated documentation files (the
# "Software"), to deal in the Software without restriction, including
# without limitation the rights to use, copy, modify, merge, publish, dis-
# tribute, sublicense, and/or sell copies of the Software, and to permit
# persons to whom the Software is furnished to do so, subject to the fol-
# lowing conditions:
#
# The above copyright notice and this permission notice shall be included
# in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
# OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABIL-
# ITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT
# SHALL THE AUTHOR BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
# WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
# IN THE SOFTWARE.
# 
# The Software shall be used for Younger than you, not Older.
# 


import xml.dom.minidom
import urllib2
import httplib
import os

import pysugarsync

end_point = "https://api.sugarsync.com/user"

def get_user():
  """
  :rtype: :class:`pysugarsync.user.User`
  :return: A connection to Amazon's SQS
  """  resp = pysugarsync.conn.do_get(end_point,{"content-type":"application/xml; charset=UTF-8"})
  dom=xml.dom.minidom.parseString(resp.read())
  resp.close()

  username=dom.getElementsByTagName("username")[0].firstChild.data
  nickname=dom.getElementsByTagName("nickname")[0].firstChild.data
  usage=dom.getElementsByTagName("usage")[0].firstChild.data
  limit=dom.getElementsByTagName("limit")[0].firstChild.data
  workspaces=dom.getElementsByTagName("workspaces")[0].firstChild.data
  syncfolders=dom.getElementsByTagName("syncfolders")[0].firstChild.data
  deleted=dom.getElementsByTagName("deleted")[0].firstChild.data
  magic_briefcase=dom.getElementsByTagName("magicBriefcase")[0].firstChild.data
  web_archive=dom.getElementsByTagName("webArchive")[0].firstChild.data
  mobile_photos=dom.getElementsByTagName("mobilePhotos")[0].firstChild.data
  albums=dom.getElementsByTagName("albums")[0].firstChild.data
  recent_activities=dom.getElementsByTagName("recentActivities")[0].firstChild.data
  received_shares=dom.getElementsByTagName("receivedShares")[0].firstChild.data
  public_links=dom.getElementsByTagName("publicLinks")[0].firstChild.data
  return User(username,nickname,usage,limit,workspaces,syncfolders,deleted,magic_briefcase,
            web_archive, mobile_photos, albums, recent_activities, received_shares, public_links)
  
class User(object):
  def __init__(self,username,nickname,usage,limit,workspaces,syncfolders,deleted,magic_briefcase,
                  web_archive, mobile_photos, albums, recent_activities, received_shares, public_links):
    self.username=username
    self.nickname=nickname
    self.limit=limit
    self.usage=usage
    self.workspaces=workspaces
    self.syncfolders=syncfolders
    self.deleted=deleted
    self.magic_briefcase=magic_briefcase
    self.web_archive =web_archive
    self.mobile_photos =mobile_photos
    self.albums =albums
    self.recent_activities =recent_activities
    self.received_shares =received_shares
    self.public_links =public_links
    self.base_url={
      "workspaces" : self.workspaces,
      "syncfolders" : self.syncfolders,
      "deleted" : self.deleted,
      "magic_briefcase" : self.magic_briefcase,
      "web_archive" : self.web_archive,
      "mobile_photos" : self.mobile_photos,
      "albums" : self.albums,
      "recent_activities" : self.recent_activities,
      "received_shares" : self.received_shares,
      "public_links" : self.public_links
    }

  def __repr__(self):
    return '<User: %s(%s) %s/%s>' % (
             self.username,self.nickname,self.usage,self.limit)

  def __str__(self):
    return '<User: %s(%s) %s/%s>' % (
             self.username,self.nickname,self.usage,self.limit)
    
